﻿using System.Drawing;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Liju.Common
{
     public class ImageHelper
    {
        /// <summary>
        /// 判断两张图片是否相同
        /// </summary>
        /// <param name="image1"></param>
        /// <param name="image2"></param>
        /// <returns></returns>
        public static bool AreImagesEqual(Image image1, Image image2)
        {
            if (image1 == null || image2 == null)
                return false;
            if (image1.Width != image2.Width || image1.Height != image2.Height)
                return false;
            if (image1.VerticalResolution != image2.VerticalResolution)
                return false;
            using (Bitmap bmp1 = new Bitmap(image1))
            using (Bitmap bmp2 = new Bitmap(image2))
            {
                for (int y = 0; y < bmp1.Height; y++)
                {
                    for (int x = 0; x < bmp1.Width; x++)
                    {
                        if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                            return false;
                    }
                }
            }

            return true;
        }
        /// <summary>
        /// 判断图片列表中是否存在改图片
        /// </summary>
        /// <param name="imgList"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        public static bool ExistSameImage(List<Image> imgList, Image image)
        {
            foreach(Image img in imgList)
            {
                if(AreImagesEqual(img, image))
                {
                    return true;
                }
            }

            return false;
        }


        public static void RotatePictureBox(PictureBox pictureBox, float angle)
        {
            // 创建一个新的Bitmap，大小与原始PictureBox相同
            Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);

            // 创建一个Graphics对象来绘制到新的Bitmap
            using (Graphics g = Graphics.FromImage(bmp))
            {
                // 设置高质量插值
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;

                // 创建一个矩阵，用于旋转
                Matrix matrix = new Matrix();
                matrix.RotateAt(angle, new PointF(bmp.Width / 2, bmp.Height / 2));

                // 应用矩阵变换
                g.Transform = matrix;

                // 将PictureBox的Image绘制到Graphics对象上，旋转后的位置
                g.DrawImage(pictureBox.Image, new PointF(0, 0));
            }

            // 将旋转后的Bitmap设置为PictureBox的Image
            pictureBox.Image = bmp;
        }

    }
}
